﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Text;
using System.IO;


namespace SnippetCreator
{
    [RunInstaller(true)]
    public partial class SnippetCreatorInstaller : Installer
    {
        public SnippetCreatorInstaller()
        {
            InitializeComponent();
        }

        public override void Install(IDictionary stateSaver)
        {
            string vs2005Path = String.Format(@"{0}\Visual Studio 2005",
                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
            string vs2008Path = String.Format(@"{0}\Visual Studio 2008",
                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
            string assemblyPath = Context.Parameters["AssemblyPath"];
            string addInBody = BuildAddinXml(assemblyPath);
            if (Directory.Exists(vs2005Path))
            {
                string addInsPath = String.Format(@"{0}\Addins", vs2005Path);
                Directory.CreateDirectory(addInsPath);
                string fullName = String.Format(@"{0}\SnippetCreator.AddIn", addInsPath);
                using (StreamWriter sw = File.CreateText(fullName))
                {
                    sw.Write(addInBody);
                }
                stateSaver.Add("vs2005Path", fullName);
            }
            if (Directory.Exists(vs2008Path))
            {
                string addInsPath = String.Format(@"{0}\Addins", vs2008Path);
                Directory.CreateDirectory(addInsPath);
                string fullName = String.Format(@"{0}\SnippetCreator.AddIn", addInsPath);
                using (StreamWriter sw = File.CreateText(String.Format(@"{0}\SnippetCreator.AddIn", addInsPath)))
                {
                    sw.Write(addInBody);
                }
                stateSaver.Add("vs2008Path", fullName);
            }

            base.Install(stateSaver);
        }

        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
            if (savedState.Contains("vs2005Path"))
            {
                string fullName = savedState["vs2005Path"].ToString();
                if (File.Exists(fullName))
                {
                    File.Delete(fullName);
                }
            }
            if (savedState.Contains("vs2008Path"))
            {
                string fullName = savedState["vs2008Path"].ToString();
                if (File.Exists(fullName))
                {
                    File.Delete(fullName);
                }
            }
        }

        private string BuildAddinXml(string assemblyPath)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
            sb.AppendLine("<Extensibility xmlns=\"http://schemas.microsoft.com/AutomationExtensibility\">");
            sb.AppendLine("<HostApplication>");
            sb.AppendLine("<Name>Microsoft Visual Studio Macros</Name>");
            sb.AppendLine("<Version>9.0</Version>");
            sb.AppendLine("</HostApplication>");
            sb.AppendLine("<HostApplication>");
            sb.AppendLine("<Name>Microsoft Visual Studio</Name>");
            sb.AppendLine("<Version>9.0</Version>");
            sb.AppendLine("</HostApplication>");
            sb.AppendLine("<HostApplication>");
            sb.AppendLine("<Name>Microsoft Visual Studio Macros</Name>");
            sb.AppendLine("<Version>8.0</Version>");
            sb.AppendLine("</HostApplication>");
            sb.AppendLine("<HostApplication>");
            sb.AppendLine("<Name>Microsoft Visual Studio</Name>");
            sb.AppendLine("<Version>8.0</Version>");
            sb.AppendLine("</HostApplication>");
            sb.AppendLine("<Addin>");
            sb.AppendLine("<FriendlyName>SnippetCreator</FriendlyName>");
            sb.AppendLine("<Description>Create intellisense snippet from selected code.</Description>");
            sb.AppendLine(String.Format(@"<Assembly>{0}</Assembly>", assemblyPath));
            sb.AppendLine("<FullClassName>SnippetCreator.Connect</FullClassName>");
            sb.AppendLine("<LoadBehavior>1</LoadBehavior>");
            sb.AppendLine("<CommandPreload>1</CommandPreload>");
            sb.AppendLine("<CommandLineSafe>0</CommandLineSafe>");
            sb.AppendLine("</Addin>");
            sb.AppendLine("</Extensibility>");
            return sb.ToString();
        }
    }
}
